//
//  ReviewInVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/3/25.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "ReviewInVC.h"

#import "POSListCell.h"

#import "PostlistModel.h"
#import "POSStore.h"

@interface ReviewInVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, assign) NSInteger VCType;

@property (nonatomic, strong) ViewHeader *secView;
@property (nonatomic, strong) ViewFoot *foot;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) NSInteger Page;
@property (nonatomic, assign) NSInteger MaxPage;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ReviewInVC

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self getPOSlist]; 
}
- (void)initCSS{
    self.Page = 1;
    self.MaxPage = 1;
    self.VCType = 1;
    self.view.backgroundColor = [UIColor colorWithHexString:@"F4F4F4"];
    self.secView = [[ViewHeader alloc]initWithPostListHeader];
    [self.secView CleanPostlistSecView:self.VCType];
    [self.view addSubview:_secView];
    
    self.foot = [[ViewFoot alloc]initWithBusinessChoose];
    self.foot.MyPOSListType = self.VCType;
    [self.view addSubview:self.foot];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, iPhoneX_Top + 25, SCREEN_WIDTH, SCREEN_HEIGHT - iPhoneX_Top - 55) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[POSListCell class] forCellReuseIdentifier:@"RPOSListCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.Page = 1;
        if (self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        [self getPOSlist];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.Page += 1;
        if (self.Page <= self.MaxPage) {
            [self getPOSlist];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.secView.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.bottom.equalTo(self.foot.mas_top);
    }];
}
- (void)getPOSlist{
    [[[POSStore alloc]init] GetMyPosReviewWithPrams:@{@"customerId":[UserDefaultsManager sharedInstance].user_id,@"current":[NSString stringWithFormat:@"%ld",self.Page],@"size":@"100",@"activeStatus":@"2"} Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.Page = [res[@"data"][@"current"] integerValue];
            self.MaxPage = [res[@"data"][@"pages"] integerValue];
            self.foot.allnumber = [res[@"data"][@"total"] integerValue];
            NSArray *arr = res[@"data"][@"records"];
            if (arr.count > 0) {
                for (NSDictionary *dic in arr) {
                    PostlistReviewModel *model = [PostlistReviewModel mj_objectWithKeyValues:dic];
                    [self.dataArr addObject:model];
                }
            }else{
                [self showMBPError:@"暂无数据"];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    POSListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RPOSListCellID"];
    if (self.dataArr.count > 0) {
        cell.typeV = self.VCType;
        cell.title.text = [NSString stringWithFormat:@"%ld",indexPath.section + 1];
        cell.indexarr = self.secView.subviews;
        cell.Rmodel = self.dataArr[indexPath.section];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


@end
